/**

 * 版权所有(C)，上海勾芒信息科技，2017，所有权利保留。
 *
 * 项目名：cards-web 
 * 文件名：:WxCompMaMessageTemplateServiceImpl.java
 * 模块说明：	
 * 修改历史：
 * 2017年10月16日 - liyang - 创建。
 * 
 */
package me.chanjar.weixin.component.api.app.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.gomore.jeecg.common.Assert;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.component.api.WxCompServiceDelegate;
import me.chanjar.weixin.component.api.app.WxCompMaMessageTemplateService;
import me.chanjar.weixin.component.bean.template.Paging;
import me.chanjar.weixin.component.bean.template.WxAppMsgTemplate;
import me.chanjar.weixin.component.bean.template.WxAppMsgTemplateLibKeyword;
import me.chanjar.weixin.component.bean.template.WxAppMsgTemplateLibTitle;

/**
 * @author:liyang
 * @time:2017年10月16日 下午2:00:57
 * @since:1.0
 */

public class WxCompMaMessageTemplateServiceImpl implements WxCompMaMessageTemplateService {

  public static final String BASE_URL = "https://api.weixin.qq.com/cgi-bin/wxopen/template";

  private final WxCompServiceDelegate delegate;

  public WxCompMaMessageTemplateServiceImpl(WxCompServiceDelegate delegate) {
    this.delegate = delegate;
  }

  @Override
  public QueryTemplateTitleResponse queryTemplateTitleList(Paging page) throws WxErrorException {
    Assert.assertArgumentNotNull(page, "page");
    String url = BASE_URL + "/library/list";
    return delegate.httpPost(url, page, QueryTemplateTitleResponse.class);
  }

  public static class QueryTemplateTitleResponse extends BaseResponse {
    private static final long serialVersionUID = -8027337758659980406L;

    private List<WxAppMsgTemplateLibTitle> list;
    @JsonProperty("total_count")
    private int totalCount;

    public List<WxAppMsgTemplateLibTitle> getList() {
      return list;
    }

    public void setList(List<WxAppMsgTemplateLibTitle> list) {
      this.list = list;
    }

    public int getTotalCount() {
      return totalCount;
    }

    public void setTotalCount(int totalCount) {
      this.totalCount = totalCount;
    }
  }

  @Override
  public List<WxAppMsgTemplateLibKeyword> getTemplateTitleKeywordById(String templateTitleId)
      throws WxErrorException {
    Assert.assertArgumentNotNull(templateTitleId, "templateTitleId");
    String url = BASE_URL + "/library/get";
    Map<String, String> req = new HashMap<String, String>(1);
    req.put("id", templateTitleId);
    GetTemplateTitleKeywordResponse resp = delegate.httpPost(url, req,
        GetTemplateTitleKeywordResponse.class);
    return resp.getKeywordList();
  }

  public static class GetTemplateTitleKeywordResponse extends BaseResponse {
    private static final long serialVersionUID = 1717771090181306533L;

    private String id;
    @JsonProperty("keyword_list")
    private List<WxAppMsgTemplateLibKeyword> keywordList;

    public String getId() {
      return id;
    }

    public void setId(String id) {
      this.id = id;
    }

    public List<WxAppMsgTemplateLibKeyword> getKeywordList() {
      return keywordList;
    }

    public void setKeywordList(List<WxAppMsgTemplateLibKeyword> keywordList) {
      this.keywordList = keywordList;
    }
  }

  @Override
  public String addTemplate(AddTemplateRequest req) throws WxErrorException {
    Assert.assertArgumentNotNull(req, "req");
    String url = BASE_URL + "/add";
    AddTemplateResponse resp = delegate.httpPost(url, req, AddTemplateResponse.class);
    return resp.getTemplateId();
  }

  public static class AddTemplateRequest {

    /** 模板标题id */
    private String id;
    /** 开发者自行组合好的模板关键词列表，关键词顺序可以自由搭配（例如[3,5,4]或[4,5,3]），最多支持10个关键词组合 */
    @JsonProperty("keyword_id_list")
    private List<Integer> keywordIdList;

    public String getId() {
      return id;
    }

    public void setId(String id) {
      this.id = id;
    }

    public List<Integer> getKeywordIdList() {
      return keywordIdList;
    }

    public void setKeywordIdList(List<Integer> keywordIdList) {
      this.keywordIdList = keywordIdList;
    }
  }

  public static class AddTemplateResponse extends BaseResponse {
    private static final long serialVersionUID = 5235740273568527348L;
    @JsonProperty("template_id")
    private String templateId;

    public String getTemplateId() {
      return templateId;
    }

    public void setTemplateId(String templateId) {
      this.templateId = templateId;
    }
  }

  @Override
  public List<WxAppMsgTemplate> queryTemplateList(Paging page) throws WxErrorException {
    Assert.assertArgumentNotNull(page, "page");
    String url = BASE_URL + "/list";
    QueryTemplateListResponse resp = delegate.httpPost(url, page, QueryTemplateListResponse.class);
    return resp.getList();
  }

  public static class QueryTemplateListResponse extends BaseResponse {
    private static final long serialVersionUID = -4784091549690996379L;
    private List<WxAppMsgTemplate> list;
    @JsonProperty("total_count")
    private int totalCount;

    public List<WxAppMsgTemplate> getList() {
      return list;
    }

    public void setList(List<WxAppMsgTemplate> list) {
      this.list = list;
    }

    public int getTotalCount() {
      return totalCount;
    }

    public void setTotalCount(int totalCount) {
      this.totalCount = totalCount;
    }
  }

  @Override
  public void removeTemplate(String templateId) throws WxErrorException {
    Assert.assertArgumentNotNull(templateId, "templateId");
    String url = BASE_URL + "/del";
    Map<String, String> req = new HashMap<String, String>(1);
    req.put("template_id", templateId);
    delegate.httpPost(url, req, QueryTemplateListResponse.class);
  }

  public static class BaseResponse implements Serializable {
    private static final long serialVersionUID = -3324612886846080408L;
    @JsonProperty("errcode")
    private int errCode;
    @JsonProperty("errmsg")
    private String errMsg;

    public int getErrCode() {
      return errCode;
    }

    public void setErrCode(int errCode) {
      this.errCode = errCode;
    }

    public String getErrMsg() {
      return errMsg;
    }

    public void setErrMsg(String errMsg) {
      this.errMsg = errMsg;
    }
  }
}
